var serverIp = "http://180.209.98.88:8101";
var table_line = 0;
var idArray = new Array();

//清除表格中的所有行
function clearTable() {
    var tb = $('#tag_table');
    var rowNum = tb.rows.length;
    for (var i = 0; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}

//按钮监听

function addNewRow() {
    var formData = new FormData();
    var tag_new_title = $('#tag_new_title').val();
    if (tag_new_title == null) {
        $("#tag_new_title_wrong").html("<span class='icon-warning-sign' style='color:red'>标签标题为空</span>");
    }
    formData.append("title", tag_new_title);
    var tag_new_Abstract = $('#tag_new_Abstract').val();
    if (tag_new_Abstract == null) {
        $("#tag_new_Abstract_wrong").html("<span class='icon-warning-sign' style='color:red'>标签概要为空</span>");
    }
    formData.append("pattern", 1);

    var tag_new_url = $('#tag_new_url').val();
    if (tag_new_url == null) {
        $("#tag_new_url_wrong").html("<span class='icon-warning-sign' style='color:red'>标签链接为空</span>");
    }
    formData.append("url", tag_new_url);
    formData.append("abstract", tag_new_Abstract);
    var tag_new_picture_url = $('#tag_new_picture_url').val();
    if (tag_new_picture_url == null) {
        $("#tag_new_picture_url_wrong").html("<span class='icon-warning-sign' style='color:red'>标签图片链接为空</span>");
    }
    formData.append("pId", 0);
    formData.append("pUrl", tag_new_picture_url);
    $.ajax({
        type: "POST",
        url: serverIp + "/tags/add.do",
        data: formData,
        contentType: false,
        processData:false,
        dataType: 'json',
        beforeSend: function () {
            $("#form-uploading").html("<span style='color:blue'>正在处理...</span>");
            return true;
        },
        success: function (result) {
            alert("啊哈，你的标签添加成功啦！");
        },
        error: function (request, errorType, errorMessage) {
            alert("[" + errorType + "] " + errorMessage);
        }

    });
}

function refresh(num) {
    $.ajax({
        type: "GET",
        url: serverIp + "/tags/list.do",
        data: {num: num},
        contentType: 'application/json',
        dataType: 'json',
        success: function (result) {

            $('#tag_table tr:not(:first)').empty("");
            table_line = 0;
            $.each(result.data, function (i, item) {

                table_line = table_line + 1;
                var id = item.id;
                var title = item.title;
                var externalUrl = item.externalUrl;
                var tagAbstract = item.tagAbstract;
                var status = item.status === 0 ? "审核中" : (item.status === 1 ? "发布" : "移除");
                var pictureId = item.pictureId;
                var pictureUrl = item.pictureUrl;
                var tag_publish_id = "tag_publish"+id;
                var tag_remove_id = "tag_remove"+id;
                var tag_delete_id = "tag_delete" + id;
                var tag_setTop_id = "tag_setTop" + id;
                var tag_edit_id  ="tag_edit" + id;
                var status_id = "status"+id;

                $("#tag_tbody").append(
                    "<tr>" +
                    "<td>" + id + "</td>" +
                    "<td>" + "<a href='" + externalUrl + "'>" + title + "</a>" + "</td>" +
                    "<td>" + tagAbstract + "</td>" +
                    "<td>" + "<a href='" + pictureUrl + "'>图片</a>" + "</td>" +
                    "<td id='status_id'>" + status + "</td>" +
                    "<td><a id='" + tag_publish_id + "' onclick='publish(\""+tag_publish_id+"\")' class='btn'><i class='icon-play'></i>发布</a></td> " +
                    "<td><a id='" + tag_remove_id + "' onclick='remove(\""+tag_remove_id+"\")' class='btn'><i class='icon-remove'></i>移除</a></td> " +
                    "<td><a id='" + tag_delete_id + "' onclick='del(\""+tag_delete_id+"\")' class='btn'><i class='icon-asterisk'></i>删除</a></td> " +
                    "<td><a id='" + tag_edit_id + "' data-toggle='modal' data-target='#editModal'  onclick='setContentBeforeEditing(\""+tag_edit_id+"\")' class='btn'><i class='icon-edit'></i>编辑</a></td> " +
                    "<td><a id='" + tag_setTop_id + "' onclick='setTop(\""+tag_setTop_id+"\")' class='btn'><i class='icon-arrow-up'></i>置顶</a></td>" +
                    "</tr>"
                )

            });
            console.log("Current line number: "+table_line);
        },
        error: function (request, errorType, errorMessage) {
            alert("[" + errorType + "] " + errorMessage);
        }

    });
}
function publish(publish_id) {
    var id = parseInt(publish_id.substr(11));//一个典型的publish_id为  tag_publish2,需要的内容为tag_publish
    $.ajax({
        type: "GET",
        url: serverIp + "/tags/publish.do",
        data: {id:id},
        contentType: 'application/json',
        dataType: 'json',
        success: function (result) {

            alert(id+"标签发布成功");
            $('#status_id').html("发布");
            refresh(10);
        },
        error:function (request, errorType, errorMessage) {
            alert("[" + errorType + "] " + errorMessage);
        }
        }
    )
}


function remove(remove_id) {
    var id = parseInt(remove_id.substr(10));//var id = parseInt(remove_id -"tag_remove");
    $.ajax({
            type: "GET",
            url: serverIp + "/tags/remove.do",
            data: {id:id},
            contentType: 'application/json',
            dataType: 'json',
            success: function (result) {
                alert(id+"标签移除成功");
                refresh(10);
            },
            error:function (request, errorType, errorMessage) {
                alert("[" + errorType + "] " + errorMessage);
            }
        }
    )
}

function del(delete_id){
    var id = parseInt(delete_id.toString().substr(10));//var id = parseInt(delete_id -"tag_delete");
    console.log(id);
    $.ajax({
            type: "GET",
            url: serverIp + "/tags/delete.do",
            data: {id:id},
            contentType: 'application/json',
            dataType: 'json',
            success: function (result) {
                alert(id+"标签删除成功");
                refresh(10);
            },
            error:function (request, errorType, errorMessage) {
                alert("[" + errorType + "] " + errorMessage);
            }
        }
    )
}

function setTop(setTop_id){
    var id = parseInt(setTop_id.substr(10));//var id = parseInt(setTop_id.toString() -"tag_setTop");
    $.ajax({
            type: "GET",
            url: serverIp + "/tags/setTop.do",
            data: {id:id},
            contentType: 'application/json',
            dataType: 'json',
            success: function (result) {
                alert(id+"标签置顶成功");
                refresh(10);
            },
            error:function (request, errorType, errorMessage) {
                alert("[" + errorType + "] " + errorMessage);
            }
        }
    )
}

function setContentBeforeEditing(edit_id){
    var id = parseInt(edit_id.substr(8));//tag_edit10
    $.ajax({
            type: "GET",
            url: serverIp + "/tags/item.do",
            data: {id:id},
            contentType: 'application/json',
            dataType: 'json',
            success: function (result) {
                var title = result.data.title;
                var abstract = result.data.tagAbstract;
                var url = result.data.externalUrl;
                var pUrl = result.data.pictureUrl;

                $("#tag_edit_title").val(title);
                $("#tag_edit_Abstract").html(abstract);
                $("#tag_edit_url").val(url);
                $("#tag_edit_picture_url").val(pUrl);
                console.log(id+"标签查询成功 "+title +"  "+abstract+ "  "+url +"  "+ pUrl);
                refresh(10);
            },
            error:function (request, errorType, errorMessage) {
                alert("[" + errorType + "] " + errorMessage);
            }
        }
    );

}

function editOneRow(){

}

// //初始调用
refresh(10);









